package com.swlz.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swlz.dao.MallTypeDAO;
import com.swlz.entity.MallProduct;
import com.swlz.entity.MallType;
import com.swlz.entity.Role;
import com.swlz.exception.CodeException;
import com.swlz.service.MallTypeService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 商城产品类别
 * ClassName : com.swlz.service.impl.MallTypeServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-27 20:53:46
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallTypeServiceImpl implements MallTypeService {
	
	private MallTypeDAO mallTypeDAO = new MallTypeDAO();
	private Logger log = LoggerFactory.getLogger(MallTypeServiceImpl.class);

	@Override
	public int add(MallType mallType) throws CodeException {
		try {
			return mallTypeDAO.insertSelective(mallType);
		} catch (SQLException e) {
			throw new CodeException(500, "添加商城产品类别错误",e);
		}
	}

	@Override
	public int update(MallType mallType) throws CodeException {
		try {
			return mallTypeDAO.updateSelective(mallType);
		} catch (SQLException e) {
			throw new CodeException(501, "修改商城产品类别错误",e);
		}
	}

	@Override
	public int delete(Long mallTypeId) throws CodeException {
		try {
			return mallTypeDAO.deleteById(mallTypeId);
		} catch (SQLException e) {
			throw new CodeException(502, "删除商城产品类别错误",e);
		}
	}

	@Override
	public MallType get(Long mallTypeId) throws CodeException {
		try {
			return mallTypeDAO.selectById(mallTypeId);
		} catch (SQLException e) {
			throw new CodeException(503, "获取商城产品类别失败",e);
		}
	}

	
	
	@Override
	public PageVO<MallType> page(MallType mallType, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = mallTypeDAO.countByModel(mallType);
			PageVO<MallType> page = new PageVO<>(total, current, rowMax);
			return page.setList(mallTypeDAO.selectByModel(mallType, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			throw new CodeException(504, "获取商城产品类别分页数据失败",e);
		}
	}

	@Override
	public List<MallType> list() {
		
		try {
			return MallTypeDAO.selectByModel(null, 0, 99);
		} catch (SQLException e) {
			e.printStackTrace();//
			throw new RuntimeException("获取商品类别失败", e);
		}
	}

	public List<MallType> list(MallType mallType, Integer index, Integer len) throws CodeException {
		List<MallType> list = null;
		try {
		 list = mallTypeDAO.selectByModel(mallType, index, len);
		} catch (SQLException e) {
			log.debug("数据检索失败");
		}
		return list;
	}

}
